<?php

/**
 * This is the model class for table "points_3857".
 *
 * The followings are the available columns in table 'points_3857':
 * @property integer $gid
 * @property string $id
 * @property string $geom
 * @property string $type
 */
class Points extends CActiveRecord
{
	public function getGeoJSON() {
		$connection= $this->getDbConnection();   // assuming you have configured a "db" connection
		// If not, you may explicitly create a connection:
		// $connection=new CDbConnection($dsn,$username,$password);
		$command=$connection->createCommand("SELECT ST_AsGeoJSON('" .  $this->geom . "')");
		$row =  $command->queryRow();
		return $row['st_asgeojson'];
	}
	public function getCentroid() {
		$connection= $this->getDbConnection();   // assuming you have configured a "db" connection
		// If not, you may explicitly create a connection:
		// $connection=new CDbConnection($dsn,$username,$password);
		$command=$connection->createCommand("SELECT ST_Y(ST_Centroid('" .  $this->geom . "'))");
		$row =  $command->queryRow();
		$command=$connection->createCommand("SELECT ST_X(ST_Centroid('" .  $this->geom . "'))");
		$col =  $command->queryRow();
		return array($row['st_y'], $col['st_x']);
	}
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return CDbConnection database connection
	 */
	public function getDbConnection()
	{
		return Yii::app()->dblima;
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'points_3857';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, type', 'length', 'max'=>10),
			array('geom', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('gid, id, geom, type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'gid' => 'Gid',
			'id' => 'ID',
			'geom' => 'Geom',
			'type' => 'Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('gid',$this->gid);
		$criteria->compare('id',$this->id,true);
		$criteria->compare('geom',$this->geom,true);
		$criteria->compare('type',$this->type,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}